package info.opensoaring.client;

import info.opensoaring.api.Flight;
import info.opensoaring.api.igc.FlightLog;
import info.opensoaring.client.rpc.FlightServiceAsync;

import java.util.Collection;
import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.debugpanel.client.DefaultCookieDebugPanelComponent;
import com.google.gwt.debugpanel.client.DefaultDebugStatisticsDebugPanelComponent;
import com.google.gwt.debugpanel.client.DefaultExceptionDebugPanelComponent;
import com.google.gwt.debugpanel.client.DefaultRawLogDebugPanelComponent;
import com.google.gwt.debugpanel.client.DefaultStatisticsModelRpcEventHandler;
import com.google.gwt.debugpanel.client.DefaultStatisticsModelStartupEventHandler;
import com.google.gwt.debugpanel.client.DelayedDebugPanelComponent;
import com.google.gwt.debugpanel.common.GwtStatisticsEventSystem;
import com.google.gwt.debugpanel.models.GwtDebugStatisticsModel;
import com.google.gwt.debugpanel.models.GwtExceptionModel;
import com.google.gwt.debugpanel.widgets.DebugPanelWidget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;

public class OpenSoaringView extends Composite implements ApplicationView,
	DebugPanelWidget.Listener {

    private GwtStatisticsEventSystem sys;
    private DefaultDebugStatisticsDebugPanelComponent panelComponent;
    private DelayedDebugPanelComponent xmlComponent;
    private DefaultRawLogDebugPanelComponent logComponent;
    private GwtDebugStatisticsModel sm;
    private GwtExceptionModel em;

    private FlightServiceAsync flightService;

    private VerticalPanel panel = new VerticalPanel();

    @Inject
    public OpenSoaringView(FlightServiceAsync pFlightService) {
	this.flightService = pFlightService;

	final Button button1 = new Button("Add");
	panel.add(button1);

	final Button button2 = new Button("Load");
	panel.add(button2);

	final Label label = new Label();
	panel.add(label);

	button1.addClickHandler(new ClickHandler() {
	    public void onClick(ClickEvent event) {
		Flight flight = new Flight("My random string" + Random.nextInt(), new Date(),
			new Date(), new Date(), new Date(), null, null, null);
		flightService.addFlight(flight, new AsyncCallback<Void>() {
		    @Override
		    public void onFailure(Throwable caught) {
			label.setText("ERROR!");
		    }

		    @Override
		    public void onSuccess(Void result) {
			flightService.getFlights(new AsyncCallback<Collection<Flight>>() {
			    @Override
			    public void onFailure(Throwable caught) {
				label.setText("ERROR!");
			    }

			    @Override
			    public void onSuccess(Collection<Flight> result) {
				label.setText("Flights :: " + result.size());
			    }
			});
		    }
		});
	    }
	});

	button2.addClickHandler(new ClickHandler() {
	    public void onClick(ClickEvent event) {
		GWT.log("Calling load flight!", null);
		flightService.analyseFlight("http://localhost:8080/test/test.igc",
			new AsyncCallback<FlightLog>() {
			    @Override
			    public void onFailure(Throwable caught) {
				label.setText("ERROR!");
			    }

			    @Override
			    public void onSuccess(FlightLog flightRecord) {
				String flight = flightRecord + "";
				label.setText(flight);
				GWT.log("Done success!", null);
			    }
			});
	    }
	});

	sys = new GwtStatisticsEventSystem();
	panelComponent = new DefaultDebugStatisticsDebugPanelComponent(null);
	xmlComponent = panelComponent.xmlComponent();
	logComponent = new DefaultRawLogDebugPanelComponent(sys);
	em = new GwtExceptionModel();

	panel.add(new DebugPanelWidget(this, true, new DebugPanelWidget.Component[] {
		panelComponent, new DefaultExceptionDebugPanelComponent(em),
		new DefaultCookieDebugPanelComponent(), logComponent, xmlComponent }));

	initWidget(panel);
    }

    @Override
    public Composite getWidget() {
	return this;
    }

    // @Override
    public void onShow() {
	panelComponent.reset(sm = new GwtDebugStatisticsModel(
		new DefaultStatisticsModelStartupEventHandler(),
		new DefaultStatisticsModelRpcEventHandler()));
	xmlComponent.reset();
	logComponent.reset();

	sys.addListener(sm, false);
	sys.addListener(em, false);
	sys.enable(true);
    }

    // @Override
    public void onReset() {
	sys.removeListener(sm);
	sys.removeListener(em);

	panelComponent.reset(null);
	sys.clearEventHistory();
    }

}
